<?php
 class BasicinfoController extends Controller{
	     
		  public $isActivated = 0;
          public function accessRules()
		 {
			return array(
			  array(
					'deny',  // deny all users
					'expression'=>'$user->rights == 2',
					'actions'=>array('adddata'),
					'message'=>'You do not have access to this page ! ! ',
				),
				array(
					'allow',  // allow all users to access 'index' and 'view' actions.
					'expression'=>'$user->rights>=2',
				),
				array(
					'deny',  // deny all users
					'users'=>array('*'),
					'message'=>'You do not have access to this page ! ! ',
				),
				
			);
		 } 
      public function actionIndex(){
		   $this->breadcrumbs=array(
               '基本资料'=>array('index'),
               '所有学生',
             );
		  
		  $classRooms  = Classroom::model()->findAll();
		  $ci  = new CDbCriteria();
		 
		 if(!empty($_GET) && (count($_GET) >= 4)){
		  foreach($_GET as $key => $value){
			  if($value == '' || $key == 'page')
				  continue;
			  if($key == 'stuNumber'){
				  $ci->params[':s'] = $_GET['stuNumber'];
				  $ci->addCondition('stuNumber = :s');		  
			  }else{
			   $p = ':'.substr($key,0,1);	  
			   $ci->params[$p] = $value;
               $ci->addCondition($key.' = '.$p);  
			  }
			  
		  }
		  
		 }
		  
		  $ci->addCondition('isPassed = 1');
		  $total = Stuinfo::model()->count($ci);
		  $pages =  new CPagination($total);
		  $pages->pageSize = 20;
		  $pages->applyLimit($ci);
		  $stus = Stuinfo::model()->findAll($ci);
		  $major = Major::model()->findAll();
		  $this->render('index',array('total'=>$total,'stus'=>$stus,'pages'=>$pages,'classRooms'=>$classRooms,'major'=>$major));
		 
		  
		  
		  
		  
	  }
	 public function actionEditstuinfo(){
		   if(is_numeric($_GET['uId'])){
			   $stu = Stuinfo::model()->find('uId = '.$_GET['uId']);
           if($stu){		      
			   $classRooms = Classroom::model()->findAll();
			   $dds = Directionaldistrict::model()->findAll();
			   $stuPer = Stuperformance::model()->find('uId = :u',array(':u'=>$stu->uId));
			   $major = Major::model()->findAll();
			   if(!$stuPer)
				   $stuPer = new Stuperformance;
			   $this->breadcrumbs=array(
               '基本资料'=>array('index'),
               '编辑 '.$stu->realName.' 学生资料',
              );
            			 
		    $this->render('editstuinfo',array('classRooms'=>$classRooms,'dds'=>$dds,'stu'=>$stu,'stuPer'=>$stuPer,'major'=>$major));
			}else{
				
				$this->redirect(array('basicinfo/index'));
			} 
		   }else{
			   $this->redirect(array('basicinfo/index'));
			   
		   }
		   
		  
	  }
	  public function actionUpdate(){
		  if(Yii::app()->request->requestType == 'POST'){
			  $stu = Stuinfo::model()->find('uId = :u',array(':u'=>Yii::app()->request->getParam('uId')));
			  if($stu){
				  $stu->attributes = $_POST;
				  $stu->isPassed = 1;
				  if($stu->save()){
					  
					  Yii::app()->user->setFlash('update',$stu->realName.' 同学的个人信息更新成功');
				  }else{
					  
					  Yii::app()->user->setFlash('update',$stu->realName.' 同学的个人信息更新成功');
				  }
				  $this->redirect(Yii::app()->createUrl('basicinfo/editstuinfo',array('uId'=>Yii::app()->request->getParam('uId'))));
			  }else{
				  $this->redirect(array('basicinfo/index'));
				  
			  }
			  
		  }else{
		   $this->logError('非法访问，管理员，学生信息更新');
		  }
		  
	  }
	  public function actionUpdateperformance(){
		  
		    if(Yii::app()->request->requestType == 'POST'){
				
			  $stuPer = Stuperformance::model()->find('uId = :u',array(':u'=>Yii::app()->request->getParam('uId')));
			  if(!$stuPer){
			     $stuPer = new Stuperformance;
			  }
				  $_POST['uId'] = Yii::app()->request->getParam('uId');
				  $stuPer->attributes = $_POST;
				  
				  if($stuPer->save()){
					  
					  Yii::app()->user->setFlash('update','成绩信息更新成功');
				  }else{
					  
					  Yii::app()->user->setFlash('update','成绩信息更新失败');
				  }
				  
				  $this->redirect(Yii::app()->createUrl('basicinfo/editstuinfo',array('uId'=>Yii::app()->request->getParam('uId'))));
			  
			  
		  }else{
		   $this->logError('非法访问，管理员，学生信息更新');
		    $this->redirect(array('basicinfo/index'));
		  }
		  
		  
	  }
	  public function actionCheckstuinfo(){
		   $this->breadcrumbs=array(
               '基本资料'=>array('index'),
               '审核学生资料',
             );
	      $classRooms  = Classroom::model()->findAll();
		  $ci  = new CDbCriteria();
		  $major = Major::model()->findAll();
		 if(!empty($_GET) && (count($_GET) >= 4)){
		  foreach($_GET as $key => $value){
			  if($value == '' || $key == 'page')
				  continue;
			  if($key == 'stuNumber'){
				  $ci->params[':s'] = $_GET['stuNumber'];
				  $ci->addCondition('user.username = :s');		  
			  }else{
			   $p = ':'.substr($key,0,1);	  
			   $ci->params[$p] = $value;
               $ci->addCondition($key.' = '.$p);  
			  }
			  
		  }
		  $ci->with = array('user');
		 }
		  
		  $ci->addCondition('isPassed = 0');
		  $total = Stuinfo::model()->count($ci);
		  $pages =  new CPagination($total);
		  $pages->pageSize = 20;
		  $pages->applyLimit($ci);
		  $stus = Stuinfo::model()->findAll($ci);
		  
		  $this->render('checkstuinfo',array('major'=>$major,'total'=>$total,'stus'=>$stus,'pages'=>$pages,'classRooms'=>$classRooms));
		  
	  }
	   public function actionAdddata(){
		   $this->breadcrumbs=array(
               '基本资料'=>array('index'),
               '添加字段',
             );
			
			 if(Yii::app()->request->requestType === 'POST'){
			     if($_POST['type'] == 'class'){
				    $classRoom  = new Classroom;
				    $classRoom->name = $_POST['cn'];
					$classRoom->majorId = $_POST['majorId'];
					if($classRoom->save()){
					   Yii::app()->user->setFlash('actionInfo',"班级添加成功");
					   
				    }else{
						$this->logAr($classRoom);
						Yii::app()->user->setFlash('actionInfo',"班级添加失败");	
					}
				 }else if($_POST['type'] == 'dd'){
					 $dd = new Directionaldistrict;
					 $dd->name = $_POST['dd'];
					 if($dd->save()){
						 Yii::app()->user->setFlash('actionInfo',"定向区县添加成功");
						}else{
						 $this->logAr($dd);
						Yii::app()->user->setFlash('actionInfo',"定向区县添加失败");	
						 
					 }
					 
				 }else if($_POST['type'] == 'major'){
					
						$major = new Major;
						$major->name = $_POST['name'];
						if($major->save()){
							
							 Yii::app()->user->setFlash('actionInfo',"专业添加成功");
						}else{
							$this->logAr($major);
							Yii::app()->user->setFlash('actionInfo',"专业添加失败");	
							
						}
					 
				 }
				$this->redirect(array('basicinfo/adddata')); 
			 }
			 $major = Major::model()->findAll();
			 $classRooms = Classroom::model()->findAll();
			 $dds  = Directionaldistrict::model()->findAll();
		  $this->render('adddata',array('classRooms'=>$classRooms,'dds'=>$dds,'major'=>$major));
		  
	  }
	   public function actionDeletedata(){
		   $this->breadcrumbs=array(
               '基本资料'=>array('index'),
               '添加字段',
             );
			 if(Yii::app()->request->requestType === 'POST'){
				 if($_POST['type'] == 'c'){
					 $classRoom = Classroom::model()->findByPk($_POST['id']);
					 if($classRoom->delete()){
					   Yii::app()->user->setFlash('actionInfo',"班级删除成功");
					   
				    }else{
						$this->logAr($classRoom);
						Yii::app()->user->setFlash('actionInfo',"班级删除失败");	
					}
					 
				 }else if($_POST['type'] == 'd'){
					 $dd =  Directionaldistrict::model()->findByPk($_POST['id']);
					
					 if($dd->delete()){
						 Yii::app()->user->setFlash('actionInfo',"定向区县删除成功");
						 
					 }else{
						 $this->logAr($dd);
						Yii::app()->user->setFlash('actionInfo',"定向区县删除失败");	
						 
					 }
					 
				 }else if($_POST['type'] == 'm'){
					 
					 $major = Major::model()->findByPk($_POST['id']);
					 if($major->delete()){
						  Yii::app()->user->setFlash('actionInfo',"专业删除成功");
						 
					 }else{
						  $this->logAr($major);
						Yii::app()->user->setFlash('actionInfo',"专业删除失败");	
						 
					 }
					 
				 }
			 }else{
				 Yii::log('invalid access','error','Application.error');
				 
			 }
			$this->redirect(array('basicinfo/adddata'));
       }
	   // 学生审核 业务流程
	   public function actionPassone(){
		   if(Yii::app()->request->isAjaxRequest){
			   $uId = $_POST['uId'];
			   $stu = Stuinfo::model()->find('uId = :u',array(':u'=>$uId));
			   if($stu){
				   $stu->isPassed = 1;
				   if($stu->save()){
						
						echo  json_encode(array('code'=>200,'msg'=>'审核成功'));
						
				   }else{
					   $this->logAr($stu);
					   echo  json_encode(array('code'=>502,'msg'=>'审核失败'));
				   }
			   }else{
				   echo  json_encode(array('code'=>404,'msg'=>'审核失败，没有找到用户'));
				   
			   }
			   
		   }else{
			   $this->logError('非法请求，basicinfo 学生审核 ajax');
			   $this->redirect(array('basicinfo/index'));
			   
		   }
		   
	   } 
	   public function actionPassmuti(){
		   $successNum = 0;
		   $successArray = array();
		   if(Yii::app()->request->isAjaxRequest){
			   $stus = '&'.$_POST['stus'];
			   $stuArray = explode('&l=',$stus);
			  foreach($stuArray as $uId){
				  if($uId == '')
					  continue;
			   $stu = Stuinfo::model()->find('uId = :u',array(':u'=>$uId));
			   if($stu){
				   $stu->isPassed = 1;
				   if($stu->save()){
					    $successArray[$successNum] = $uId;
						$successNum++;
				    }else{
					   $this->logAr($stu);
					   echo  json_encode(array('code'=>502,'msg'=>$stu->realName.' 审核失败'.'<br>成功的学生数目：'.$successNum,'successArray'=>$successArray));
					   return;
				   }
			    }else{
				   echo  json_encode(array('code'=>404,'msg'=>'审核失败，没有找到用户'.'<br>成功的学生数目：'.$successNum,'successArray'=>$successArray));
				   return;
			    }
			  }
		   echo  json_encode(array('code'=>200,'msg'=>'审核成功的学生数目：'.$successNum,'successArray'=>$successArray));
		   }else{
			   $this->logError('非法请求，basicinfo 多位 学生审核 ajax');
			   $this->redirect(array('basicinfo/index'));
			   
		   }
		   
	   }
	   public function actionUnpassmuti(){
		   $successNum = 0;
		   $successArray = array();
		   if(Yii::app()->request->isAjaxRequest){
			   $stus = '&'.$_POST['stus'];
			   $stuArray = explode('&l=',$stus);
			  foreach($stuArray as $uId){
				  if($uId == '')
					  continue;
			   $stu = Stuinfo::model()->find('uId = :u',array(':u'=>$uId));
			   if($stu){
				   $stuN = new Stunotification;
				   $stuN->uId = $uId;
				   $stuN->detailId = $stu->stuInfoId;
				   $stuN->module = 1; //1 -- 个人信息 2 -- 综合奖学金 3 -- GSP 4---综合素质 5 -- 学生报名
				   $stuN->comment = '同学，您的个人信息没有通过审核，请修改后，再次提交';
				   $stuN->addtime = date('Y-m-d H:i:s');
				   $stuN->isRead = 0;
				   
				   $stu->isPassed = 1;
				   if($stu->save()&&$stuN->save()){
					    $successArray[$successNum] = $uId;
						$successNum++;
				    }else{
					   $this->logAr($stu);
					   echo  json_encode(array('code'=>502,'msg'=>$stu->realName.' 添加 不通过 审核失败'.'<br>成功的学生数目：'.$successNum,'successArray'=>$successArray));
					   return;
				   }
			    }else{
				   echo  json_encode(array('code'=>404,'msg'=>'添加不通过审核失败，没有找到用户'.'<br>成功的学生数目：'.$successNum,'successArray'=>$successArray));
				   return;
			    }
			  }
		   echo  json_encode(array('code'=>200,'msg'=>'添加不通过审核成功的学生数目：'.$successNum,'successArray'=>$successArray));
		   }else{
			   $this->logError('非法请求，basicinfo 多位 学生审核 ajax');
			   $this->redirect(array('basicinfo/index'));
			   
		   }
		   
	   }
	    public function actionAddcomment(){
		   if(Yii::app()->request->isAjaxRequest){
			   $uId = $_POST['uId'];
			   $stuInfoId = $_POST['stuInfoId'];
			   $comment = $_POST['comment'];
			   $stuInfo = Stuinfo::model()->find('uId = :u',array(':u'=>$uId));
			   if($stuInfo){
				   $stuN = new Stunotification;
				   $stuN->uId = $stuInfo->uId;
				   $stuN->detailId = $stuInfo->stuInfoId;
				   $stuN->module = 1; //1 -- 个人信息 2 -- 综合奖学金 3 -- GSP 4---综合素质 5 -- 学生报名
				   $stuN->comment = $comment;
				   $stuN->addtime = date('Y-m-d H:i:s');
				   $stuN->isRead = 0;
				   
				   $stu = Stuinfo::model()->find('uId = :u',array(':u'=>$uId));
				   $stu->isPassed = 1;
				   
				   if($stuN->save()&&$stu->save()){
					  echo  json_encode(array('code'=>200,'msg'=>'添加成功'));
				   }else{
						$this->logAr($stuN);
						echo  json_encode(array('code'=>502,'msg'=>'添加失败'));
				   }
				   
			   }else{
				   
				   echo  json_encode(array('code'=>404,'msg'=>'没有找到这个条目'));
			   }
			   
			   
			   
		   }else{
			   $this->logError('非法请求，basicinfo 学生评论添加 ajax');
			   $this->redirect(array('basicinfo/index'));
			   
		   }
		   
	   }
	   
	   public function actionExport(){
		   
		    $ci  = new CDbCriteria();
		 
		 if(!empty($_GET) && (count($_GET) >= 4)){
		  foreach($_GET as $key => $value){
			  if($value == '' || $key == 'page')
				  continue;
			  if($key == 'stuNumber'){
				  $ci->params[':s'] = $_GET['stuNumber'];
				  $ci->addCondition('stuNumber = :s');		  
			  }else{
			   $p = ':'.substr($key,0,1);	  
			   $ci->params[$p] = $value;
               $ci->addCondition($key.' = '.$p);  
			  }
			  
		  }
		  
		 }
		  
		  $ci->addCondition('isPassed = 1');
		  $total = Stuinfo::model()->count($ci);
		  $stus = Stuinfo::model()->findAll($ci);
		  
		  Yii::$enableIncludePath = false;
		  Yii::import('application.extensions.phpExcel.PHPExcel',1);
		  PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );
		  $objPHPExcel = new PHPExcel();
		  $objPHPExcel->getProperties()->setCreator("kezhi Company")
							 ->setLastModifiedBy("第二师范学院学生信息管理系统")
							 ->setTitle(date('Y-m-d H:i:s').'学生信息导出')
							 ->setSubject("学生信息导出")
							 ->setDescription("学生信息导出")
							 ->setKeywords("学生信息导出")
							 ->setCategory("export stuinfo file");
		 
		  $objPHPExcel->setActiveSheetIndex(0)
					->setCellValue('A1', '学号')
					->setCellValue('B1', '姓名')
					->setCellValue('C1', '年级')
					->setCellValue('D1', '班级')
					->setCellValue('E1', '专业')
					->setCellValue('F1', '性别')
					->setCellValue('G1', '身份证号')
					->setCellValue('H1', '手机')
					->setCellValue('I1', '民族')
					->setCellValue('J1', '银行名称')
					->setCellValue('K1', '银行卡号')
					->setCellValue('L1', '家庭地址')
					->setCellValue('M1', '寝室号')
					->setCellValue('N1', '定向区县')
					->setCellValue('O1', '父母电话')
					->setCellValue('P1', '备注');
		  $i = 2;  
		 foreach($stus as $s){
			 $objPHPExcel->setActiveSheetIndex(0)
					->setCellValue('A'.$i, ' '.$s->user->username)
					->setCellValue('B'.$i, $s->realName)
					->setCellValue('C'.$i, $s->grade)
					->setCellValue('D'.$i, $s->classRoom)
					->setCellValue('E'.$i, $s->major)
					->setCellValue('F'.$i, $s->gender)
					->setCellValue('G'.$i, ' '.$s->idNum)
					->setCellValue('H'.$i, $s->phoneNum)
					->setCellValue('I'.$i, $s->nation)
					->setCellValue('J'.$i, $s->bankName)
					->setCellValue('K'.$i, ' '.$s->bankNum)
					->setCellValue('L'.$i, $s->homePlace)
					->setCellValue('M'.$i, $s->dormNum)
					->setCellValue('N'.$i, $s->directionalDistrict)
					->setCellValue('O'.$i, $s->parentNum)
					->setCellValue('P'.$i, $s->remark);
					$i++;
			 
		 }			
		  
		  $objPHPExcel->setActiveSheetIndex(0);
          $this->downloadExcel(date('Y-m-d').'学生信息导出',$objPHPExcel);
		 
	   }
 }
?>